package cn.edu.cqu.bysj.wm.gui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.JDialog;
import javax.swing.JProgressBar;


import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

public class ProgressDialog extends JDialog {

	/**
	 * launch the application
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ProgressDialog dialog = new ProgressDialog();
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * create the progress dialog
	 */
	public ProgressDialog() {
		setTitle("running");
		setBounds(0, 0, 100, 10);
		
		JProgressBar progressBar = new JProgressBar();
		progressBar.setIndeterminate(true);
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
				.addComponent(progressBar, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
				);
		groupLayout.setVerticalGroup(
				groupLayout.createParallelGroup(Alignment.TRAILING)
				.addComponent(progressBar, GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
				);
		getContentPane().setLayout(groupLayout);
	}

	public ProgressDialog(SwingWorkExecute task) {
		this();
		this.setVisible(true);

		task.execute(this);
		this.pack();
		setToScreenCenter(this);
	}

	public static void setToScreenCenter(Component window){
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension windowSize = window.getSize();
		window.setLocation((screenSize.width-windowSize.width)/2,(screenSize.height-windowSize.height)/2);
	}
	public static interface SwingWorkExecute{
		public void execute(ProgressDialog progressDialog);
	}
}
